package wps.component.elementsetting {
import flash.events.Event;

import mx.binding.utils.BindingUtils;
import mx.collections.ArrayCollection;
import mx.collections.ArrayList;
import mx.controls.Alert;
import mx.core.UIComponent;
import mx.filters.BaseFilter;

import spark.filters.BevelFilter;
import spark.filters.BlurFilter;
import spark.filters.DropShadowFilter;
import spark.filters.GlowFilter;

import wps.component.IDrawingElement;
import wps.constant.FilterConst;
import wps.model.SettingFilter;
import wps.skin.HeadlessDataGridSkin;
import wps.util.CollectionUtil;

public class SettingFiltersDataGrid extends SettingDataGrid {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var seq:int;

    //----------------------------------
    // selectedFilter
    //----------------------------------

    private var _selectedFilter:BaseFilter;

    [Bindable("selectedFilterChange")]
    /**
     * 选中的filter，BaseFilter对象
     */
    public function get selectedFilter():BaseFilter {
        return _selectedFilter;
    }

    public function set selectedFilter(selectedFilter:BaseFilter):void {
        _selectedFilter = selectedFilter;
        dispatchEvent(new Event("selectedFilterChange"));
    }

    //----------------------------------
    // maxFilters
    //----------------------------------

    private var _maxFilters:int;

    /**
     * 最多能添加maxFilters个filter
     */
    public function get maxFilters():int {
        return _maxFilters;
    }

    public function set maxFilters(maxFilters:int):void {
        _maxFilters = maxFilters;
    }

    //----------------------------------
    // full
    //----------------------------------

    private var _full:Boolean;

    [Bindable]
    /**
     * 是否到达可以添加filter的最大数目
     */
    public function get full():Boolean {
        return _full;
    }

    public function set full(full:Boolean):void {
        _full = full;
    }

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    public function SettingFiltersDataGrid() {
        super();

        setStyle("skinClass", Class(HeadlessDataGridSkin));
        maxFilters = FilterConst.MAXFILTERS;

        // 在selectedItem变化时，或dataProvider为空时，
        // selectedFilterSetter都需要重新设置
        BindingUtils.bindSetter(selectedFilterSetter, this, "selectedItem");
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    private function selectedFilterSetter(value:Object):void {
        selectedFilter = value ? SettingFilter(value).filter : null;
    }

    override public function checkEnable():void {
        enabled = target ? true : false;
    }

    override protected function propertySetter(value:Object):void {
        if (target) {
            dataProvider = target[targetProperty];
            if (dataProvider) {
                updateElementFilters();
                full = dataProvider.length >= maxFilters;
            } else {
                full = false;
            }
        } else {
            dataProvider = null;
            selectedFilter = null;
            full = true;
        }
    }

    /**
     * 添加filter，只能是"D,G,B,L"这四种中的一种
     */
    public function addFilter(type:String):void {
        var sf:SettingFilter = new SettingFilter();

        switch (type) {
            case FilterConst.TYPE_DROPSHADOW:
                sf.icon = FilterConst.ICON_DROPSHADOW;
                sf.filter = new DropShadowFilter();
                break;

            case FilterConst.TYPE_GLOW:
                sf.icon = FilterConst.ICON_GLOW;
                sf.filter = new GlowFilter();
                break;

            case FilterConst.TYPE_BLUR:
                sf.icon = FilterConst.ICON_BLUR;
                sf.filter = new BlurFilter();
                break;

            case FilterConst.TYPE_BEVEL:
                sf.icon = FilterConst.ICON_BEVEL;
                sf.filter = new BevelFilter();
                break;
        }
        sf.type = type;
        sf.name = FilterConst.nameMap[type];

        addFilterToElement(sf);
    }

    private function addFilterToElement(filter:SettingFilter):void {
        var element:IDrawingElement = IDrawingElement(target);

        if (!element.settingFilters) {
            element.settingFilters = new ArrayCollection();
        }

        element.settingFilters.addItem(filter);
        selectedItem = filter;

        filter.seq = ++seq;
        if (filter.valid) {
            var filters:Array = target.filters;
            filters.push(filter.filter);
            target.filters = filters;
        }

        if (element.settingFilters.length >= maxFilters) {
            full = true;
        }
    }

    /**
     * 移除一个filter
     */
    public function removeFilter(filter:SettingFilter):void {
        // 移除选中的filter时，清空selectedFilter
        if (selectedItem == filter) {
            selectedFilter = null;
        }

        var element:IDrawingElement = IDrawingElement(target);
        var index:int = element.settingFilters.getItemIndex(filter);
        element.settingFilters.removeItemAt(index);

        // 更新编号
        updateSequence();

        // 只有移除有效的filter时，才更新元素的filters
        if (filter.valid) {
            updateElementFilters();
        }

        if (element.settingFilters.length < maxFilters) {
            full = false;
        }
    }

    /**
     * 当元素的filter集合发生状态变化时<br/>
     * 如：移除，状态变化，顺序变化<br/>
     * 把新的filters状态更新到元素的filters属性
     */
    public function updateElementFilters():void {
        var arr:Array = new Array();
        for each (var filter:SettingFilter in dataProvider) {
            if (filter.valid) {
                arr.push(filter.filter);
            }
        }
        target.filters = arr;
    }

    private function updateSequence():void {
        seq = 0;
        for each (var sf:SettingFilter in dataProvider) {
            sf.seq = ++seq;
        }
    }

    //--------------------------------------------------------------------------
    //
    // Event Handlers
    //
    //--------------------------------------------------------------------------

}

}
